/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AbsenceSummaryPanel.java
 *
 * Created on 18-Aug-2010, 23:16:16
 */

package TimeRecording.forms;

import TimeRecording.tables.WorkingSummary;
import TimeRecording.tables.Employee;
import java.awt.Color;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 *
 * @author david
 */
public class AbsenceSummaryPanel extends javax.swing.JPanel
        implements PropertyChangeListener {

    // <editor-fold defaultstate="collapsed" desc="WorkingPatternMaster">

    public WorkingSummary getWorkingPatternMaster() {
	return workingPatternMaster;
    }

    @SuppressWarnings("unchecked")
    public void setWorkingPatternMaster(WorkingSummary workingPatternMaster) {

        // Adhere to standard listener behaviour

        this.workingPatternMaster.removePropertyChangeListener(this);
        workingPatternMaster.addPropertyChangeListener(this);

        WorkingSummary oldWorkingPatternMaster = this.workingPatternMaster;
        this.workingPatternMaster = workingPatternMaster;
        firePropertyChange(
                "workingPatternMaster", oldWorkingPatternMaster, workingPatternMaster);
        
        propertyChange(null);

        setDetailsDisplayed(workingPatternMaster != null &&
                workingPatternMaster.getEmployees() != null);
    } // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Employee">

    public Employee getEmployee() {
	return employee;
    }

    @SuppressWarnings("unchecked")
    public void setEmployee(Employee employee) {

        // Adhere to standard listener behaviour

        this.employee.removePropertyChangeListener(this);
        employee.addPropertyChangeListener(this);

        Employee oldEmployee = this.employee;
        this.employee = employee;
        firePropertyChange(
                "employee", oldEmployee, employee);

        propertyChange(null);

    } // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="detailsDisplayed">
    private boolean detailsDisplayed;

    public boolean isDetailsDisplayed() {
        return detailsDisplayed;
    }

    public void setDetailsDisplayed(boolean detailsDisplayed) {
        boolean oldDetailsDisplayed = this.detailsDisplayed;
        this.detailsDisplayed = detailsDisplayed;
        firePropertyChange("detailsDisplayed", oldDetailsDisplayed, detailsDisplayed);
    } // </editor-fold>

    /** Creates new form AbsenceSummaryPanel */
    public AbsenceSummaryPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        workingPatternMaster = new TimeRecording.tables.WorkingSummary();
        employee = new TimeRecording.tables.Employee();
        grantedLabel = new javax.swing.JLabel();
        allowance = new javax.swing.JFormattedTextField();
        takenLabel = new javax.swing.JLabel();
        taken = new javax.swing.JFormattedTextField();
        approvedLabel = new javax.swing.JLabel();
        approved = new javax.swing.JFormattedTextField();
        netYetApprovedLabel = new javax.swing.JLabel();
        notYetApproved = new javax.swing.JFormattedTextField();
        remainingLabel = new javax.swing.JLabel();
        remaining = new javax.swing.JFormattedTextField();
        sicknessLabel = new javax.swing.JLabel();
        sicknessTaken = new javax.swing.JFormattedTextField();
        unitsPanel = new javax.swing.JPanel();
        unitsCombo = new javax.swing.JComboBox();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(TimeRecording.TimeRecordingApp.class).getContext().getResourceMap(AbsenceSummaryPanel.class);
        setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("Form.border.title"))); // NOI18N
        setName("Form"); // NOI18N
        setLayout(new java.awt.GridBagLayout());

        grantedLabel.setText(resourceMap.getString("grantedLabel.text")); // NOI18N
        grantedLabel.setName("grantedLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        add(grantedLabel, gridBagConstraints);

        allowance.setEditable(false);
        allowance.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));
        allowance.setName("allowance"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        add(allowance, gridBagConstraints);

        takenLabel.setText(resourceMap.getString("takenLabel.text")); // NOI18N
        takenLabel.setName("takenLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        add(takenLabel, gridBagConstraints);

        taken.setEditable(false);
        taken.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));
        taken.setName("taken"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        add(taken, gridBagConstraints);

        approvedLabel.setText(resourceMap.getString("approvedLabel.text")); // NOI18N
        approvedLabel.setName("approvedLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        add(approvedLabel, gridBagConstraints);

        approved.setEditable(false);
        approved.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));
        approved.setName("approved"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        add(approved, gridBagConstraints);

        netYetApprovedLabel.setText(resourceMap.getString("netYetApprovedLabel.text")); // NOI18N
        netYetApprovedLabel.setName("netYetApprovedLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 4, 4, 4);
        add(netYetApprovedLabel, gridBagConstraints);

        notYetApproved.setEditable(false);
        notYetApproved.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));
        notYetApproved.setName("notYetApproved"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 4, 4, 4);
        add(notYetApproved, gridBagConstraints);

        remainingLabel.setText(resourceMap.getString("remainingLabel.text")); // NOI18N
        remainingLabel.setName("remainingLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 4, 4, 4);
        add(remainingLabel, gridBagConstraints);

        remaining.setEditable(false);
        remaining.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));
        remaining.setName("remaining"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 4, 4, 4);
        add(remaining, gridBagConstraints);

        sicknessLabel.setText(resourceMap.getString("sicknessLabel.text")); // NOI18N
        sicknessLabel.setName("sicknessLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 4, 4, 4);
        add(sicknessLabel, gridBagConstraints);

        sicknessTaken.setEditable(false);
        sicknessTaken.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));
        sicknessTaken.setText(resourceMap.getString("sicknessTaken.text")); // NOI18N
        sicknessTaken.setName("sicknessTaken"); // NOI18N
        sicknessTaken.setPreferredSize(new java.awt.Dimension(28, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 4, 4, 4);
        add(sicknessTaken, gridBagConstraints);

        unitsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("unitsPanel.border.title"))); // NOI18N
        unitsPanel.setName("unitsPanel"); // NOI18N

        unitsCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Days", "Hours" }));
        unitsCombo.setName("unitsCombo"); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${workingPatternDisplayed}"), unitsCombo, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        unitsCombo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                unitsComboItemStateChanged(evt);
            }
        });
        unitsPanel.add(unitsCombo);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 4, 4);
        add(unitsPanel, gridBagConstraints);

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void unitsComboItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_unitsComboItemStateChanged
        propertyChange(null);
    }//GEN-LAST:event_unitsComboItemStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFormattedTextField allowance;
    private javax.swing.JFormattedTextField approved;
    private javax.swing.JLabel approvedLabel;
    private TimeRecording.tables.Employee employee;
    private javax.swing.JLabel grantedLabel;
    private javax.swing.JLabel netYetApprovedLabel;
    private javax.swing.JFormattedTextField notYetApproved;
    private javax.swing.JFormattedTextField remaining;
    private javax.swing.JLabel remainingLabel;
    private javax.swing.JLabel sicknessLabel;
    private javax.swing.JFormattedTextField sicknessTaken;
    private javax.swing.JFormattedTextField taken;
    private javax.swing.JLabel takenLabel;
    private javax.swing.JComboBox unitsCombo;
    private javax.swing.JPanel unitsPanel;
    private TimeRecording.tables.WorkingSummary workingPatternMaster;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    public void propertyChange(PropertyChangeEvent evt) {

        double divisor;
        if (unitsCombo.getSelectedIndex() == 0) {
            divisor = workingPatternMaster.getHoursWorkedPerDay();
            if (divisor == 0) {
                divisor = 1;
                unitsCombo.setBackground(Color.RED);
            } else {
                unitsCombo.setBackground(Color.WHITE);
            }
        } else {
            divisor = 1;
            unitsCombo.setBackground(Color.WHITE);
        }

        allowance.setValue(workingPatternMaster.getAnnualLeaveGranted() / divisor);
        remaining.setValue(workingPatternMaster.getAnnualLeaveRemaining() / divisor);
        approved.setValue(workingPatternMaster.getAnnualLeaveApproved() / divisor);
        notYetApproved.setValue(workingPatternMaster.getAnnualLeaveUnapproved() / divisor);
        taken.setValue(workingPatternMaster.getAnnualLeaveTaken() / divisor);

        if (workingPatternMaster.getEmployees() != null) {
            sicknessTaken.setValue(workingPatternMaster.
                    getEmployees().getSickness() / divisor);
        }
    }
}